package yuyicman.bitmaps 
{
	import caurina.transitions.Tweener;
	import com.flashandmath.bitmaps.BitmapTransformer;
	import flash.display.BitmapData;
	import flash.display.DisplayObject;
	import flash.display.MovieClip;
	import flash.display.Shape;
	import flash.events.Event;
	import flash.geom.Rectangle;
	
	import yuyicman.debug.Debugger;
	/**
	 * ...
	 * @author Yuyic.
	 */
	public class BitmapPaper extends MovieClip
	{
		
		
		protected var _segmentsList		:Array;
		protected var _vDiv				:int;
		protected var _hDiv				:int;
		protected var _xStep			:Number;
		protected var _yStep			:Number;
		protected var _width			:Number;
		protected var _height			:Number;
		
		
		protected var _bdTarget1			:DisplayObject;
		protected var _bdTarget2			:DisplayObject;
		
		
		public function get segmentsList():Array {
			return _segmentsList;
		}
		
		public function BitmapPaper( vDiv:int, hDiv:int, width:Number, height:Number )
		{
			
			_segmentsList = [];
			
			_vDiv = vDiv;
			_hDiv = hDiv;
			
			_width = width;
			_height = height;
			
			_xStep = width / hDiv;
			_yStep = height / vDiv;
			
			
			_init();
			
		}
		
		private function _init():void {
			
			var bs:BitmapSegment;
			var i:int, j:int;
			
			for ( i = 0; i <= _hDiv; i++ ) {
				
				_segmentsList[i] = [];
				
				for ( j = 0; j <= _vDiv; j++ ) {
					
					bs = new BitmapSegment();
					bs.paper = this;
					bs._x = i * _xStep - _width / 2;
					bs._y = j * _yStep - _height / 2;
					
					bs.init();
					
					addChildAt( bs, i * _hDiv + j );
					
					_segmentsList[i].push( bs );
					
					if ( i > 0 ) {
						bs.segmentsList.push( _segmentsList[i - 1][j] );
					}
					
					if ( j > 0 ) {
						bs.segmentsList.push( _segmentsList[i][j - 1] );
					}
					
					if ( i > 0 && j > 0 ) {
						
						bs.v1 = _segmentsList[i - 1][j - 1];
						bs.v2 = _segmentsList[i][j - 1];
						bs.v3 = _segmentsList[i - 1][j];
						bs.v4 = bs;
						bs.imgRect = new Rectangle( (i - 1) / _hDiv, (j - 1) / _vDiv, 1 / _hDiv, 1 / _vDiv );
						
					}
					
				}
				
			}
			
		}
		
		
		public function render(e:Event=null):void {
			
			var i:int, j:int;
			
			graphics.clear();
			
			_callEverySegments( function( bs:BitmapSegment, i:int, j:int ):void {
				bs.x = Math.floor( bs.x );
				bs.y = Math.floor( bs.y );
				bs.render();
			} );
			
		}
		
		public function setBd2( target:DisplayObject ):void
        {
			
			_bdTarget2 = target;
			
            _callEverySegments( function( bs:BitmapSegment, i:int, j:int ):void {
				bs.setBd2( target );
			} );
			
        }
		
		
		public function setBd1( target:DisplayObject ):void
        {
			
			_bdTarget1 = target;
			
			_callEverySegments( function( bs:BitmapSegment, i:int, j:int ):void {
				bs.setBd1( target );
			} );
			
			
        }
		
		public function swapBd():void {
			
			var temp:* = _bdTarget1;
			setBd1( _bdTarget2 );
			setBd2( temp );
			
		}
		
		
		public function setPaperSize( rect:Rectangle ):void
        {
			
			log( "setPaperSize WIDTH:" + rect.width + " HEIGHT:" + rect.height );
			
            _width = rect.width;
            _height = rect.height;
			
            _xStep = _width / _hDiv;
            _yStep = _height / _vDiv;
			
			_callEverySegments( function( bs:BitmapSegment, i:int, j:int ):void {
				bs._x = bs._x2 = Math.floor(i * _xStep - _width / 2);
				bs._y = bs._y2 = Math.floor(j * _yStep - _height / 2);
				bs.init();
			} );
			
            
        }
		
		
		
		
		//////////////////////////////	getter && setter
		
		
		
		public function set lineVisible( visible:Boolean ):void {
			
			
			_callEverySegments( function( bs:BitmapSegment, i:int, j:int ):void {
				bs.lineVisible = visible;
			} );
			
			
		}
		
		public function set smoothOn( value:Boolean ):void {
			
			_callEverySegments( function( bs:BitmapSegment, i:int, j:int ):void {
				bs.smoothOn = value;
			} );
			
		}
		
		
		public static function getTemp( color:uint = 0 ):DisplayObject {
			
			var shape:Shape = new Shape();
			shape.graphics.beginFill(color);
			shape.graphics.drawRect(0, 0, 10, 10);
			shape.graphics.endFill();
			return shape;
			
		}
		
		//////////////////////////////
		
		
		/// call Every Bitmap Segments
		
		protected function _callEverySegments( func:Function ):void {
			
			var i:int, j:int;
			
			for ( i = 0; i <= _hDiv; i++ ) {
				
				for ( j = 0; j <= _vDiv; j++ ) {
					
					func.apply( this, [ segmentsList[i][j],i,j ] );
					
				}
				
			}
			
			
		}
		
		private function log( msg:*, priority:int = 0 ):void {
			Debugger.log( "[BitmapPaper] " + String(msg), priority );
		}
		
		

		
	}

}